21.5 Establishing the High-Resolution Timer Subsystem
The High-Resolution Timer (hrtimer) subsystem is engineered within the Linux kernel to deliver nanosecond-level timekeeping precision, systematically superseding the legacy Timer Wheel architecture that was strictly constrained to millisecond-level resolution governed by the system's HZ compile-time frequency.
Traditional low-resolution timers rely entirely on the periodic clock tick rhythm; if the system configuration sets HZ=100, the maximum precision evaluates only to 10 ms, and even under a HZ=1000 configuration, the tracking granularity remains capped at 1 ms. Conversely, the high-resolution timer subsystem achieves sharp sub-microsecond precision by directly programming the low-level hardware clock event device to assert hardware interrupts at the exact calculated nanosecond checkpoint required. It functions analogously to a digital stopwatch capable of programmatic nanosecond-level alarm programming.
Diverging from the multi-tiered wheel sectors deployed by legacy timers, the hrtimer engine manages a localized control center designated as struct hrtimer_cpu_base on each individual CPU core, which encapsulates an array of independent interval red-black trees categorized by distinct clock types. The choice to organize these tracking structures via red-black trees is specifically driven to optimize lookup execution performance. The kernel constantly evaluates and accesses exclusively the leftmost node (rb_leftmost) across the red-black tree layout, because that element represents the absolute next timer scheduled to expire.
All pending high-resolution timers attached to the red-black tree are automatically sorted in chronological order based on their explicit expiration timestamps. Whenever a new timer is dynamically registered into the tree, if its calculated expiration boundary falls earlier than the active leftmost entry, the timekeeping engine instantly reprograms the physical hardware timer to target this newly updated expiration checkpoint.
During the early stages of the system boot sequence, the high-resolution tracking capabilities are not activated immediately; instead, the subsystem transitions through two distinct operational phases. Before the underlying physical clock sources and clock event devices are fully registered and validated as stable, the high-resolution subsystem operates in a temporary low-resolution emulation mode, where its expiration accuracy remains bounded by the standard clock tick rhythm. The moment the kernel verifies that the platform's hardware components support high-precision scheduling and exhibit acceptable stability metrics, it hot-swaps the subsystem over to full high-resolution mode. Under this active high-precision state, the system's baseline periodic clock tick itself is driven and dispatched as a payload callback by a dedicated high-resolution timer.
Once this high-resolution timer framework is established, kernel developers can instantiate individual timers via the interface hrtimer_init(). Within this configuration interface, the timer argument references the target struct hrtimer structure that preserves the metadata of the defined timer, while which_clock identifies the targeted physical timebase source. The supported clock sources encompass CLOCK_MONOTONIC (a monotonically increasing timebase that can never step backward), CLOCK_REALTIME (the system's real-world time reflecting international UTC alignment), CLOCK_BOOTTIME (a monotonic timebase that concurrently tracks the duration spent in low-power sleep states), and CLOCK_TAI (International Atomic Time). The mode argument dictates the precise execution context allocated to run the timer's callback function. To balance nanosecond-level accuracy against general system processing throughput, the execution of high-resolution timer callbacks is partitioned across hard interrupt contexts and soft interrupt contexts. Tasks demanding absolute precision execute their callbacks directly within the hard clock interrupt service routine by configuring the execution flag HRTIMER_MODE_HARD (the system default setting). Conversely, if the mode parameter enables the flag HRTIMER_MODE_SOFT—indicating that the callback logic is computationally intensive or prone to blocking—the kernel offloads and queues the execution block onto the HRTIMER_SOFTIRQ softirq vector, minimizing the duration for which local hard interrupts remain disabled on that core.
Following the structural configuration of a high-resolution timer instance, the developer hooks the custom callback function address onto the function member of the hrtimer descriptor, as demonstrated below:
my_timer.function = &my_callback_function;
The function hrtimers_init() is responsible for assembling the underlying multi-core framework required to drive these high-precision timed interrupts. This routine is defined within the core source file git/kernel/time/hrtimer.c, and its definition is implemented as follows:
void __init hrtimers_init(void)
{
hrtimers_prepare_cpu(smp_processor_id());
open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
}
This function acts as the definitive foundation layer for the high-resolution timer subsystem, unlocking nanosecond-level timekeeping across the entire operating system image. It initially dispatches hrtimers_prepare_cpu() to bootstrap the baseline data structures inside struct hrtimer_cpu_base for the current executing processor core (typically the Boot CPU, core 0). The kernel allocates an independent, private hrtimer_cpu_base structure to each CPU core, ensuring that each processor exclusively manipulates its own local red-black timer trees.
hrtimers_prepare_cpu() iterates systematically through each independent clock_base member managed inside the local hrtimer_cpu_base container, initializing the localized spinlocks dedicated to protecting each timebase. It concurrently calls timerqueue_init_head() to configure the root nodes of the individual red-black trees, programmatically setting up the anchor paths clock_base[i]->active->rb_root. Finally, it initializes the diverse per-CPU operational tracking status flags.
Two distinct clock_base structural tracking slots map back to a single shared timebase hardware source. For instance, both clock_base[HRTIMER_BASE_MONOTONIC] and clock_base[HRTIMER_BASE_MONOTONIC_SOFT] interface with the single global timebase source CLOCK_MONOTONIC. Consequently, each active clock source maps to exactly two local red-black trees on the core: one reserved to handle hard interrupt context timers and the other reserved to handle soft interrupt context timers (since the hrtimer structure embeds a struct timerqueue_node element, every individual high-resolution timer functions fundamentally as an active node within the red-black tree topology). Every high-resolution timer instantiated on that CPU core is eventually sorted and attached onto its matching active tracking tree based on its explicit capability flags.
The statement open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq) formally registers the HRTIMER_SOFTIRQ vector (which historically maps to software interrupt vector index 8) within the kernel's global softirq subsystem, assigning the function pointer hrtimer_run_softirq directly to the action member of the softirq_vec[8] entry. This registration tells the core kernel: "from this point forward, whenever a softirq event raises the HRTIMER_SOFTIRQ vector, immediately execute the handler logic implemented inside hrtimer_run_softirq."